<?

function _template_fieldset_options_begin ($container) {

	print <<<HTML
<tr>
	<td class="css_fieldset_controls" align="right" colspan="2">

HTML;
}

function _template_fieldset_options_end ($container) {

	print <<<HTML
	</td>
</tr>

HTML;
}

function _template_fieldset_option ($container) {

	#<button class="css_fieldset_button" type="submit" name="{$container->get_variable ("action_field")}" value="{$container->get_variable ("option")}">{$container->get_variable ("label")}</button>
	$onclick = ($container->get_variable ("option") == "cancel")
		? "location='{$_SERVER["HTTP_REFERER"]}'"
		: "document.{$container->get_variable ("fieldset_id")}.{$container->get_variable ("action_field")}.value='{$container->get_variable ("option")}'; this.form.submit ();";

	print <<<HTML
		<input class="css_fieldset_button" type="button" onclick="javascript:{$onclick}" value="{$container->get_variable ("label")}">

HTML;
}

function _template_fieldset_label ($container) {

	if ($container->get_variable ("comment")) {

		$comment = <<<HTML
<br><a class="css_fieldset_comment">{$container->get_variable ("comment")}</a>
HTML;
	}
	print <<<HTML
	<td class="css_fieldset_label">:: {$container->get_variable ("label")}{$comment}</td>

HTML;
}

function _template_fieldset_value ($container) {

	$field = $container->get_variable ("field");

	if ($field->get_variable ("input") == "select" &&
			!$field->get_variable ("relation"))

		$value = _template_fieldset_select_value ($container, $field);
	elseif ($field->get_variable ("input") == "password")

		$value = "*************";
	#elseif ($field->get_variable ("multiple_relation"))

	#	$value = _template_fieldset_multiple_value ($container, $field);
	elseif ($field->get_variable ("input") == "image")

		$value = _template_fieldset_image_value ($container, $field);
	else $value = $container->get_variable ("value");

	$value = preg_replace ("/\\n/", "<BR>", $value);
	#$value = preg_replace ("/ /", "&nbsp;", $value);
	print <<<HTML
	<td class="css_fieldset_value">$value</td>

HTML;
}

function _template_fieldset_image_value ($container, $field) {

	$fieldset = $container->get_variable ("fieldset");
	$value = $fieldset->get_field ($fieldset->get_option
			("primary_key"))->get_variable ("value");
	$path = $field->get_variable ("path");

	if (!file_exists ("{$_SERVER["DOCUMENT_ROOT"]}/{$path}/{$value}.jpg"))
		return "&nbsp;";

	return <<<HTML
	<img src="/scale.php?path={$path}{$value}.jpg&scale={$field->get_variable ("scale")}">
HTML;

}

function _template_fieldset_multiple_value ($container, $field) {

	$multiple = $field->get_variable ("multiple_callback");

	print $multiple;
	if (function_exists ($multiple))

		return $multiple ($container->get_variable ("fieldset"), $field);
}

function _template_fieldset_select_value ($container, $field) {

	$options = $field->get_variable ("options");
	$columns = $field->get_variable ("columns");

	#reset ($options);

	foreach ($options as $option) {

		if ($option[$columns["key"]] == $field->get_variable ("value"))
		{ $return = $option[$columns["value"]]; break; }
	}

	reset ($options);

	return $return;
}

function _template_fieldset_input ($container) {

	$field = $container->get_variable ("field");

	if (preg_match ("/hidden/", $field->get_variable ("input")) || 
			$field->get_variable ("primary_key"))

		_template_fieldset_hidden ($container);
	elseif ($field->get_variable ("input") == "textarea")

		_template_fieldset_textarea ($container);
	elseif ($field->get_variable ("input") == "image")

		_template_fieldset_image ($container);
	elseif ($field->get_variable ("input") == "date_time")

		_template_fieldset_date_time ($container);
	elseif ($field->get_variable ("input") == "date")

		_template_fieldset_date ($container);
	elseif ($field->get_variable ("input") == "time")

		_template_fieldset_time ($container);
	elseif ($field->get_variable ("input") == "select")

		_template_fieldset_select ($container);
	else _template_fieldset_field ($container);
}

function _template_fieldset_image ($container) {
	
	print <<<HTML
	<td class="css_fieldset_value"><input class="css_fieldset_value" type="file" name="{$container->get_variable ("name")}" value="{$container->get_variable ("value")}"></td>

HTML;
}

function _app_date_time ($var) {

	return _app_date ($var) . ' ' . _app_time ($var);
}

function _app_date ($var) {

	if (isset ($_REQUEST[$var . "_yr"]))

		return $_REQUEST[$var . "_yr"] . "-" .
				$_REQUEST[$var . "_mo"] . "-" .
				$_REQUEST[$var . "_dy"];
}

function _app_time ($var) {

	if (isset ($_REQUEST[$var . "_hr"]))

		return $_REQUEST[$var . "_hr"] . ":" .
				$_REQUEST[$var . "_mn"] . ":" .
				$_REQUEST[$var . "_sc"];
}

function _app_get_date ($time=0) {

	$time = localtime ($time ? $time : time(), true);
	return array (($time["tm_mon"] + 1) < 10 ?
				"0" . ($time["tm_mon"] + 1) :
				($time["tm_mon"] + 1),
			$time["tm_mday"] < 10 ?
				"0" . $time["tm_mday"] :
				$time["tm_mday"],
			$time["tm_year"] + 1900);
}

function _app_get_time ($time=0) {

	$time = localtime ($time ? $time : time(), true);
	return array ($time["tm_hour"] < 10 ?
				"0" . $time["tm_hour"] :
				$time["tm_hour"],
			$time["tm_min"] < 10 ?
				"0" . $time["tm_min"] :
				$time["tm_min"],
			$time["tm_sec"] < 10 ?
				"0" . $time["tm_sec"] :
				$time["tm_sec"]);
}

function _app_get_date_time ($time=0) {

	$time = localtime ($time ? $time : time(), true);
	return array (($time["tm_mon"] + 1) < 10 ?
				"0" . ($time["tm_mon"] + 1) :
				($time["tm_mon"] + 1),
			$time["tm_mday"] < 10 ?
				"0" . $time["tm_mday"] :
				$time["tm_mday"],
			$time["tm_year"] + 1900,
			$time["tm_hour"] < 10 ?
				"0" . $time["tm_hour"] :
				$time["tm_hour"],
			$time["tm_min"] < 10 ?
				"0" . $time["tm_min"] :
				$time["tm_min"],
			$time["tm_sec"] < 10 ?
				"0" . $time["tm_sec"] :
				$time["tm_sec"]);
}

function _app_mk_date ($value) {

	if (!$value) return;
	$date = split ("-",$value);
	return mktime (0,0,0,$date[1],$date[2],$date[0]);
}

function _app_mk_time ($value) {

	if (!$value) return;
	$time = split (":",$value);
	return mktime ($time[0],$time[1],$time[2],0,0,0);
}

function _app_mk_date_time ($value) {

	if (!$value) return;
	$_date = split (" ",$value);
	$date = split ("-",$_date[0]);
	$time = split (":",$_date[1]);


	if ($_date[0] == "" && $_time[0] == "") return mktime();

	return mktime ($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
}

function _template_fieldset_date_time ($container) {

	$name = $container->get_variable ("name");
	$value = $container->get_variable ("value");

	if (!$value && isset ($_REQUEST[$name . "_yr"])) {

		list ($_REQUEST[$name . "_mo"],
				$_REQUEST[$name . "_dy"],
				$_REQUEST[$name . "_yr"],
				$_REQUEST[$name . "_hr"],
				$_REQUEST[$name . "_mn"],
				$_REQUEST[$name . "_sc"]) = _app_get_date_time ();
	} else list ($_REQUEST[$name . "_mo"],
				$_REQUEST[$name . "_dy"],
				$_REQUEST[$name . "_yr"],
				$_REQUEST[$name . "_hr"],
				$_REQUEST[$name . "_mn"],
				$_REQUEST[$name . "_sc"]) = _app_get_date_time (_app_mk_date_time ($value));
	
	print <<<HTML
	<td class="css_fieldset_value">

HTML;

	_template_get_date ($container);
	_template_get_time ($container);

	print <<<HTML
	</td>

HTML;
}

function _template_fieldset_date ($container) {

	$name = $container->get_variable ("name");
	$value = $container->get_variable ("value");

	if (!$value && isset ($_REQUEST[$name . "_yr"])) {

		list ($_REQUEST[$name . "_mo"],
				$_REQUEST[$name . "_dy"],
				$_REQUEST[$name . "_yr"]) = _app_get_date ();
	} else list ($_REQUEST[$name . "_mo"],
				$_REQUEST[$name . "_dy"],
				$_REQUEST[$name . "_yr"]) = _app_get_date (_app_mk_date ($value));
	
	print <<<HTML
	<td class="css_fieldset_value">

HTML;

	_template_get_date ($container);

	print <<<HTML
	</td>

HTML;
}

function _template_fieldset_time ($container) {

	$name = $container->get_variable ("name");
	$value = $container->get_variable ("value");

	if (!$value && isset ($_REQUEST[$name . "_hr"])) {

		list ($_REQUEST[$name . "_hr"],
				$_REQUEST[$name . "_mn"], 
				$_REQUEST[$name . "_sc"]) = _app_get_time ();
	} else list ($_REQUEST[$name . "_hr"],
				$_REQUEST[$name . "_mn"], 
				$_REQUEST[$name . "_sc"]) = _app_get_time (_app_mk_time ($value));
	
	print <<<HTML
	<td class="css_fieldset_value">

HTML;

	_template_get_time ($container);

	print <<<HTML
	</td>

HTML;
}

function _template_get_date ($container) {

	$name = $container->get_variable ("name");

	print <<<HTML
		<select name="{$name}_mo" class="css_fieldset_dt">

HTML;
	
	foreach (array (
			1=>"January",
			2=>"February",
			3=>"March",
			4=>"April",
			5=>"May",
			6=>"Jun",
			7=>"July",
			8=>"August",
			9=>"September",
			10=>"October",
			11=>"November",
			12=>"December",
		) as $key => $val) {

		if ($_REQUEST[$name . "_mo"] == $key) $select = " selected";
		else $select = "";

		print <<<HTML
			<option{$select} value="{$key}">$val</option>
HTML;

	}

	print <<<HTML
		</select>

HTML;

	print <<<HTML
		<select name="{$name}_dy" class="css_fieldset_dt">

HTML;
	
	for ($i = 1; $i < 32; $i++) {

		if ($_REQUEST[$name . "_dy"] == $i) $select = " selected";
		else $select = "";

		$val = $i < 10 ? "0" . $i : $i;
		print <<<HTML
			<option{$select} value="{$i}">$val</option>

HTML;
	}

	print <<<HTML
		</select>

HTML;

	print <<<HTML
		<select name="{$name}_yr" class="css_fieldset_dt">

HTML;
	
	for ($i = 2020; $i > 1965; $i--) {

		if ($_REQUEST[$name . "_yr"] == $i) $select = " selected";
		else $select = "";

		print <<<HTML
			<option{$select} value="{$i}">$i</option>

HTML;
	}

	print <<<HTML
		</select><br>

HTML;

}

function _template_get_time ($container) {

	$name = $container->get_variable ("name");

	print <<<HTML
		<select name="{$name}_hr" class="css_fieldset_dt">

HTML;
	
	for ($i = 0; $i < 24; $i++) {

		if ($_REQUEST[$name . "_hr"] == $i) $select = " selected";
		else $select = "";

		$val = $i < 10 ? "0" . $i : $i;
		print <<<HTML
			<option{$select} value="{$val}">$val</option>

HTML;
	}
	
	print <<<HTML
		</select>

HTML;

	print <<<HTML
		<select name="{$name}_mn" class="css_fieldset_dt">

HTML;
	
	for ($i = 0; $i < 60; $i++) {

		if ($_REQUEST[$name . "_mn"] == $i) $select = " selected";
		else $select = "";

		$val = $i < 10 ? "0" . $i : $i;
		print <<<HTML
			<option{$select} value="{$val}">$val</option>

HTML;
	}

	print <<<HTML
		</select>

HTML;

	print <<<HTML
		<select name="{$name}_sc" class="css_fieldset_dt">

HTML;
	
	for ($i = 0; $i < 60; $i++) {

		if ($_REQUEST[$name . "_sc"] == $i) $select = " selected";
		else $select = "";

		$val = $i < 10 ? "0" . $i : $i;
		print <<<HTML
			<option{$select} value="{$val}">$val</option>

HTML;
	}

	print <<<HTML
		</select>

HTML;

}

function _template_fieldset_select ($container) {

	$field = $container->get_variable ("field");
	$fieldset = $container->get_variable ("fieldset");
	$select_options = $field->get_variable ("options");
	$select_options_params = $field->get_variable ("options_params");
	$columns = $field->get_variable ("columns");

	if (is_array ($select_options)) $options = $select_options;
	elseif (function_exists ($select_options))

		$options = $select_options ($select_options_params, $columns);

	if ($field->get_variable ("multiple")) {
		$multiple = " multiple size=\"" .
				$field->get_variable ("multiple") . "\"";
		$set_multiple = "[]";
	}

	print <<<HTML
	<td class="css_fieldset_value">
		<select class="css_fieldset_value" name="{$container->get_variable ("name")}{$set_multiple}"{$multiple}>

HTML;

	foreach ($options as $option) {

		$select = "";

		if (is_array ($field->get_variable ("value"))) {

			$grep = "/^{$option[$columns["key"]]}$/";

			if (preg_grep ($grep, $field->get_variable ("value")))

				$select = " selected";
		} elseif ($option[$columns["key"]] ==
				$field->get_variable ("value"))

			$select = " selected";

		print <<<HTML
			<option{$select} value="{$option[$columns["key"]]}">{$option[$columns["value"]]}</option>

HTML;
	}

	print <<<HTML
		</select>
	</td>

HTML;
}

function _template_fieldset_hidden ($container) {

	print <<<HTML
<input class="css_fieldset_value" type="hidden" name="{$container->get_variable ("name")}" value="{$container->get_variable ("value")}">

HTML;

}

function _template_fieldset_textarea ($container) {

	$field = $container->get_variable ("field");

	print <<<HTML
	<td class="css_fieldset_value"><textarea rows="{$field->get_variable ("rows")}" class="css_fieldset_value" name="{$container->get_variable ("name")}">{$container->get_variable ("value")}</textarea></td>

HTML;
}

function _template_fieldset_field ($container) {

	print <<<HTML
	<td class="css_fieldset_value"><input class="css_fieldset_value" type="{$container->get_variable ("input")}" name="{$container->get_variable ("name")}" value="{$container->get_variable ("value")}"></td>

HTML;
}

function _template_fieldset_entry_begin ($container) {

	if ($container->get_variable ("field") && $error_msg =
			$container->get_variable ("field")->get_error ()) {
		print <<<HTML
<tr><td class="css_fieldset_validation_error" colspan="2">$error_msg</td></tr>

HTML;
	}
	print <<<HTML
<tr>

HTML;
}

function _template_fieldset_entry_end ($container) {

	print <<<HTML
</tr>

HTML;
}

function _template_fieldset_begin ($container) {

	if ($container->get_variable ("enctype"))
			$enctype = " enctype={$container->get_variable ("enctype")}";
	print <<<HTML
<form name="{$container->get_variable ("name")}" action="{$container->get_variable ("action")}" method="POST"{$enctype}>
<table class="css_fieldset_control" cellpadding="3" cellspacing="1" border="0">
<input type="hidden" name="{$container->get_variable ("action_field")}">

HTML;

	if ($fs_title = $container->get_variable ("fs_title")) {

	print <<<HTML
<tr><td class="css_list_title" colspan="2">{$fs_title}</td></tr>

HTML;
	}

	$fieldset = $container->get_variable ("fieldset");

	if (is_array ($fieldset))
		foreach ($fieldset as $set) {
			_template_fieldset_error
					($set->get_option ("sql_query_error"));
			foreach ($set->_errors as $errmsg) 
	
				_template_fieldset_error ($errmsg);
		}
	else {
	
		_template_fieldset_error
				($fieldset->get_option ("sql_query_error"));
		foreach ($fieldset->_errors as $errmsg)

			_template_fieldset_error ($errmsg);
	}
}

function _template_fieldset_error ($error_msg) {

	if (!isset ($error_msg)) return;
	print <<<HTML
<tr><td class="css_fieldset_validation_error" colspan="2">$error_msg</td></tr>
HTML;
}

function _template_fieldset_end ($container) {

	print <<<HTML
</table>
</form>

HTML;
}

?>
